package com.atguigu.transform;

import com.atguigu.bean.WaterSensor;
import com.atguigu.functions.mapFunctionImpl;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class fliterDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        DataStreamSource<WaterSensor> sensorDS = env.fromElements(
                new WaterSensor("s1", 1L, 11),
                new WaterSensor("s2", 2L, 22),
                new WaterSensor("s3", 3L, 33),
                new WaterSensor("s4", 4L, 44)
        );

        //todo filter：true保留 ，false过滤掉
        SingleOutputStreamOperator<WaterSensor> filterDS = sensorDS.filter(
                new FilterFunction<WaterSensor>() {
                    @Override
                    public boolean filter(WaterSensor waterSensor) throws Exception {
//                        return waterSensor.getId().equals("s1");
                        return "s1".equals(waterSensor.getId()); // 常量写在前面，避免开发过程中空指针异常
                    }
                }
        );
        filterDS.print();

        env.execute();
    }
}
